//
//  HomeViewController.m
//  Project
//
//  Created by liting on 2025/8/29.
//

#import "HomeViewController.h"
#import "LZFileLoader.h"
#import "HomeRepository.h"
#import "BannerModel.h"
#import "ItemModel.h"
#import "LZBannerView.h"
#import "LZImageLoader.h"
#import "NovelCell.h"
#import "BaseFlutterViewController.h"
#import "FlutterWrapperVC.h"
#import "RecommendVC.h"


// 注册Cell标识符
static NSString *const kCollectionCellIdentifier = @"NovelCell";
static NSString *const kCollectionHeaderIdentifier = @"CollectionHeaderView";


@interface HomeViewController ()<LZBannerViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) LZBannerView *bannerView;
@property (nonatomic, strong) NSDictionary *userData;
@property (nonatomic, strong) UIButton *backButton1;
@property (nonatomic, copy) NSArray<BannerModel*>* bannerData;
@property (nonatomic, strong) NSMutableArray<ItemModel*>* listData;
@property (nonatomic, assign) int page;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavTitle:@"首页"];
    [self hiddenBackBtn];
    [self setupCollectionView];
    [self setupNativeLabel];
    [self loadNewData];

    // 引擎预热
//    [[FlutterEngineManager sharedInstance] prewarmEngine];
}


- (void)setupCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 12;
    layout.minimumLineSpacing = 12;
    layout.sectionInset = UIEdgeInsetsMake(12, 12, 12, 12);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    [self setupCollectionViewWithLayout:layout];

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.alwaysBounceVertical = YES;
    [self.collectionView registerClass:[NovelCell class]
            forCellWithReuseIdentifier:kCollectionCellIdentifier];
    [self.collectionView registerClass:[UICollectionReusableView class]
            forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                   withReuseIdentifier:kCollectionHeaderIdentifier];
    [self addRefreshHeaderWithScrollView:self.collectionView];
    [self addLoadMoreFooterWithScrollView:self.collectionView];
    [self.view addSubview:self.collectionView];
}

- (void)loadNewData {
    self.page = 1;
    [self loadBannerData];
    [self loadListData];
}

- (void)loadMoreData {
    self.page++;
    [self loadListData];
}


- (void)loadBannerData {
    self.bannerData = [[HomeRepository sharedLoader]getBannerData];
}

-(void)loadListData{
    if (self.page == 1) {
        self.listData = [NSMutableArray array];
    }
    NSArray<ItemModel*>* arr = [[HomeRepository sharedLoader]getListDataWithPage:self.page];
    [self.listData addObjectsFromArray:arr];
    [self.collectionView reloadData];
    [self endRefreshing];
    if (arr.count == 0) {
        [self endRefreshingWithNoMoreData];
    }
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.listData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NovelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionCellIdentifier forIndexPath:indexPath];

    if (indexPath.item < self.listData.count) {
        ItemModel *item = self.listData[indexPath.item];
        [cell configureWithModel:item];
    }

    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];

    if (indexPath.item < self.listData.count) {
        ItemModel *item = self.listData[indexPath.item];
        NSDictionary *params = @{@"imageUrl": item.image};
        FlutterWrapperVC *flutterVC = [[FlutterWrapperVC alloc] initWithRoute:@"/detail" params:params navigationBarHidden:YES];
        flutterVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:flutterVC animated:YES];
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat padding = 12;
    CGFloat collectionViewWidth = collectionView.bounds.size.width;
    CGFloat availableWidth = collectionViewWidth - padding * 3;
    CGFloat itemWidth = availableWidth / 2;
    CGFloat itemHeight = itemWidth * 1.4;

    return CGSizeMake(itemWidth, itemHeight);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(collectionView.bounds.size.width, 200);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(0, 0);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kCollectionHeaderIdentifier forIndexPath:indexPath];
    for (UIView *subview in headerView.subviews) {
        [subview removeFromSuperview];
    }

    self.bannerView = [LZBannerView bannerViewWithFrame:headerView.bounds delegate:self];
    self.bannerView.autoScrollInterval = 4.0;
    self.bannerView.currentPageDotColor = [UIColor blueColor];
    self.bannerView.pageDotColor = [[UIColor grayColor] colorWithAlphaComponent:0.6];
    if(self.bannerData.count>0){
        [self.bannerView setBanners:self.bannerData];
    }
    [headerView addSubview:self.bannerView];
    return headerView;

}


@end
